package ui;

import dao.StudentDao;
import dao.StudentDaoArrayList;
import domain.Student;

import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

/**
 * @Author: 朱新舜
 * @Date: 2020-04-26 11:52:19
 */
public class StudentTest {
    private static Scanner scanner = new Scanner(System.in);
    private static StudentDao dao = new StudentDaoArrayList();

    public static void main(String[] args) {
        while (true) {
            System.out.println("-----------欢迎光临------------");
            System.out.println("1、添加学生信息");
            System.out.println("2、查询学生信息");
            System.out.println("3、修改学生信息");
            System.out.println("4、删除学生信息");
            System.out.println("5、退出系统");
            System.out.println("请输入你的选择:");
            /*接收用户键盘收入，保存在变量i*/
            int i = scanner.nextInt();
            switch (i) {
                case 1:
                    insertUI();
                    break;
                case 2:
                    selectUI();
                    break;
                case 3:
                    updateUI();
                    break;
                case 4:
                    deleteUI();
                    break;
                case 5:
                    System.exit(0);
                    break;
                default:
                    System.out.println("输入有误！请重新输入");
            }
        }
    }


    private static void insertUI() {
        System.out.println("----保存界面----");
        System.out.println("请输入用户id:");
        int id = scanner.nextInt();
        System.out.println("请输入学生姓名：");
        scanner.nextLine();
        String name = scanner.nextLine();
        System.out.println("请输入学生性别：");
        String sex = scanner.nextLine();
        System.out.println("请输入学生年龄：");
        int age = scanner.nextInt();
        if (name.equals("")) {
            System.out.println("输入有误：学生姓名不能为空");
        }
        if (sex.equals("")) {
            System.out.println("输入有误：学生性别不能为空");
        }
        if (age <= 0) {
            System.out.println("输入有误：学生年龄不能为负数或等于零");
        }
        if (!name.equals("") && !sex.equals("") && age > 0) {
            Student student = new Student(id, name, sex, age);
            dao.insert(student);
        }
    }

    private static void selectUI() {
        System.out.println("----查询界面----");
        System.out.println("所有学生信息如下：");
        List<Student> list = dao.selectAll();
        Iterator<Student> iterator = list.iterator();
        while (iterator.hasNext()) {
            Student student = iterator.next();
            System.out.println(student);
        }

    }

    private static void updateUI() {
        System.out.println("----更新界面----");
        System.out.println("请输出要更新的学生id：");
        int id = scanner.nextInt();
        Student student = dao.selectById(id);
        if (student == null) {
            System.out.println("要更新的学生不存在！");
        } else {
            System.out.println("要更新的学生信息如下：");
            System.out.println(student);
            System.out.println("请输入更新后的学生姓名：");
            scanner.nextLine();
            String name = scanner.nextLine();
            System.out.println("请输入学生性别：");
            String sex = scanner.nextLine();
            System.out.println("请输入学生年龄：");
            int age = scanner.nextInt();
            Student s = new Student(id, name, sex, age);
            dao.update(id, s);
        }

    }


    private static void deleteUI() {
        System.out.println("----删除界面----");
        System.out.println("请输入要删除的学生id：");
        int id = scanner.nextInt();
        dao.delete(id);
    }
}
